// AWTWindow.cpp: implementation of the AWTWindow class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTWindow.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTWindow, AWTContainer)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTWindow::AWTWindow()
{
	m_focusMgr = new AWTFocusManager(this);
	m_visible = FALSE;
	m_windowListener = NULL;
}

AWTWindow::AWTWindow(AWTFrame* parent)
{
	m_focusMgr = new AWTFocusManager(this);
	m_visible = FALSE;
	m_windowListener = NULL;

	m_parent = parent;
//	m_parent->addOwnedWindow(this);
	setLayout(new AWTBorderLayout());
}

AWTWindow::~AWTWindow()
{
	if (m_focusMgr != NULL)
		delete m_focusMgr;
//	if (m_layoutManager != NULL)
//		delete m_layoutManager;
}

void AWTWindow::pack() 
{
	AWTContainer* parent = (AWTContainer*)m_parent;
	if (parent != NULL && parent->getPeer() == NULL) 
	{
	  parent->addNotify();
	}
	if (m_peer == NULL) 
	{
		addNotify();
	}
	setSize(getPreferredSize());
	validate();
}

void AWTWindow::show() 
{
	AWTContainer* parent = (AWTContainer*)m_parent;
	if (parent != NULL && parent->getPeer() == NULL) 
	{
	  parent->addNotify();
	}
	if (m_peer == NULL) 
	{
		addNotify();
	}
	validate();
		
	if (!m_visible) 
		setVisible(TRUE);	

/*
	if (visible) {
	    toFront();
	} else {
	    super.show();
	}
*/
}

void AWTWindow::dispose() 
{
//	setVisible(FALSE);
	removeNotify();
	removeAll();
/*
 	if (parent != null) 
	{
	  Frame parent = (Frame) this.parent;
	  parent.removeOwnedWindow(this);
 	} 
  postWindowEvent(WindowEvent.WINDOW_CLOSED);
*/
}

void AWTWindow::processWindowEvent(AWTWindowEvent* e) 
{
  if (m_windowListener != NULL) 
	{
    switch(e->getId()) {
      case AWTWindowEvent::WINDOW_OPENED:
        m_windowListener->windowOpened(e);
        break;
      case AWTWindowEvent::WINDOW_CLOSING:
        m_windowListener->windowClosing(e);
        break;
      case AWTWindowEvent::WINDOW_CLOSED:
        m_windowListener->windowClosed(e);
        break;
      case AWTWindowEvent::WINDOW_ICONIFIED:
        m_windowListener->windowIconified(e);
        break;
      case AWTWindowEvent::WINDOW_DEICONIFIED:
        m_windowListener->windowDeiconified(e);
        break;
      case AWTWindowEvent::WINDOW_ACTIVATED:
        m_windowListener->windowActivated(e);
        break;
      case AWTWindowEvent::WINDOW_DEACTIVATED:
        m_windowListener->windowDeactivated(e);
        break;
      default:
        break;
    }
  }
}

void AWTWindow::addWindowListener(AWTWindowListener* l) 
{
  m_windowListener = AWTEventMulticaster::add(m_windowListener, l);
//  newEventsOnly = true;
}

void AWTWindow::removeWindowListener(AWTWindowListener* l) 
{
	m_windowListener = AWTEventMulticaster::remove(m_windowListener, l);
}
